package han.chensing.latos.maker.windows.dialog.sample;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Array;

import java.io.File;

import han.chensing.latos.maker.res.Res;
import han.chensing.latos.maker.V;
import han.chensing.latos.common.level.sample.Sample;
import han.chensing.latos.common.level.sample.SampleSet;
import han.chensing.latos.maker.swing.FileSelector;
import han.chensing.latos.maker.windows.dialog.base.BaseDialog;

public class SampleDialog extends BaseDialog {

    TextButton closeButton;
    TextButton sampleSetAdd,sampleSetRemove;
    TextButton sampleAdd,sampleRemove;
    TextButton importFromMidi;
    List<SampleSet> sampleSetList;
    List<Sample> sampleList;
    ScrollPane sampleSetScrollPane;
    ScrollPane sampleScrollPane;
    TextField sampleSetNameTextField;
    TextField sampleNameTextField,sampleTimeTextField;

    public SampleDialog() {
        super("Samples");
        setSize(800,600);
        Skin skin = Res.get().get("skinBasic", Skin.class);
        closeButton=new TextButton("Done",skin);
        closeButton.setSize(75,30);
        closeButton.setPosition(
                getWidth()/2f-closeButton.getWidth()/2f,
                closeButton.getHeight()/2f);
        closeButton.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                SampleDialog.this.remove();
            }
        });
        addActor(closeButton);

        sampleSetList= new List<>(skin);
        sampleSetScrollPane=new ScrollPane(sampleSetList,skin);
        sampleSetScrollPane.setBounds(10,85,200,480);
        sampleSetScrollPane.setScrollbarsVisible(true);
        addActor(sampleSetScrollPane);
        updateSampleSets();

        sampleSetNameTextField=new TextField("",skin);
        sampleSetNameTextField.setBounds(240,535,200,30);
        updateSampleSetData();
        addActor(sampleSetNameTextField);

        sampleList=new List<>(skin);
        sampleScrollPane=new ScrollPane(sampleList,skin);
        sampleScrollPane.setBounds(240,85,300,440);
        addActor(sampleScrollPane);

        sampleSetAdd=new TextButton("+",skin);
        sampleSetAdd.setBounds(10,50,30,30);
        addActor(sampleSetAdd);
        sampleSetRemove=new TextButton("-",skin);
        sampleSetRemove.setBounds(50,50,30,30);
        addActor(sampleSetRemove);
        sampleSetRemove.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                int selectedIndex = sampleSetList.getSelectedIndex();
                java.util.List<SampleSet> sampleSets = V.project.getSampleData().getSampleSets();
                if (selectedIndex<0||selectedIndex>= sampleSets.size()) return;
                sampleSets.remove(selectedIndex);
                updateSampleSets();
                updateSampleSetData();
            }
        });
        sampleSetAdd.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                int selectedIndex = sampleSetList.getSelectedIndex();
                java.util.List<SampleSet> sampleSets = V.project.getSampleData().getSampleSets();
                if (selectedIndex<0||selectedIndex>= sampleSets.size())
                    selectedIndex=0;
                sampleSets.add(selectedIndex,new SampleSet());
                updateSampleSets();
                updateSampleSetData();
            }
        });

        sampleAdd=new TextButton("+",skin);
        sampleAdd.setBounds(240,50,30,30);
        addActor(sampleAdd);
        sampleRemove=new TextButton("-",skin);
        sampleRemove.setBounds(280,50,30,30);
        addActor(sampleRemove);
        sampleRemove.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                SampleSet selected = sampleSetList.getSelected();
                if (selected==null) return;
                java.util.List<Sample> samples = selected.getSamples();
                int selectedIndex = sampleList.getSelectedIndex();
                if (selectedIndex<0||selectedIndex>=samples.size()) return;
                samples.remove(selectedIndex);
                updateSamples();
                updateSamplesScrollBar();
            }
        });
        sampleAdd.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                SampleSet selected = sampleSetList.getSelected();
                if (selected==null) return;
                java.util.List<Sample> samples = selected.getSamples();
                int selectedIndex = sampleList.getSelectedIndex();
                if (selectedIndex<0||selectedIndex>=samples.size())
                    selectedIndex=0;
                samples.add(selectedIndex,new Sample());
                updateSamples();
                updateSamplesScrollBar();
            }
        });

        sampleNameTextField=new TextField("",skin);
        sampleNameTextField.setBounds(550,85,200,30);
        sampleTimeTextField=new TextField("",skin);
        sampleTimeTextField.setBounds(550,125,200,30);
        sampleTimeTextField.setTextFieldFilter((textField, c) -> Character.isDigit(c));
        sampleNameTextField.setTextFieldListener((textField, c) -> {
            Sample sample=sampleList.getSelected();
            if (sample==null) return;
            sample.setName(textField.getText());
            updateSampleData();
        });
        sampleTimeTextField.setTextFieldListener((textField, c) -> {
            Sample sample=sampleList.getSelected();
            if (sample==null)return;
            try{
                sample.setTime(Long.parseLong(textField.getText()));
            }catch (Exception ignore){
                sample.setTime(0);
            }
            updateSampleData();
        });
        addActor(sampleNameTextField);
        addActor(sampleTimeTextField);

        importFromMidi=new TextButton("Import from midi",skin);
        importFromMidi.setBounds(550,495,200,30);
        importFromMidi.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                File file = FileSelector.selectFile(FileSelector.midiFileFilter);
                if (file==null)
                    return;
                V.container.addWindow(new SampleImportFromMidiDialog(file));
            }
        });

        addActor(importFromMidi);

        moveToCenter();

        sampleList.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                updateSampleData();
            }
        });
        sampleSetList.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                updateSampleSetData();
                updateSamples();
                updateSamplesScrollBar();
            }
        });
        sampleSetNameTextField.setTextFieldListener((textField, c) -> {
            SampleSet selected = sampleSetList.getSelected();
            if (selected==null) return;
            selected.setName(textField.getText());
            updateSampleSetData();
        });
    }

    public void updateSamples(){
        sampleList.clearItems();
        sampleList.setItems();
        SampleSet selected = sampleSetList.getSelected();
        if (selected==null) return;
        Array<Sample> items = sampleList.getItems();
        java.util.List<Sample> samples = selected.getSamples();
        for (Sample sample:samples)
            items.add(sample);
    }

    public void updateSamplesScrollBar(){
        SampleSet selected = sampleSetList.getSelected();
        if (selected==null) return;
        Array<Sample> items = sampleList.getItems();
        float height = items.size * sampleList.getItemHeight();
        sampleList.setHeight(height);
        sampleScrollPane.updateVisualScroll();
        sampleList.invalidateHierarchy();
    }

    public void updateSampleSets(){
        sampleSetList.clearItems();
        Array<SampleSet> items = sampleSetList.getItems();
        java.util.List<SampleSet> sampleSets = V.project.getSampleData().getSampleSets();
        for (SampleSet sampleSet:sampleSets)
            items.add(sampleSet);
        sampleSetList.setHeight(items.size*sampleSetList.getItemHeight());
        sampleSetScrollPane.updateVisualScroll();
        sampleSetList.invalidateHierarchy();
    }

    public void updateSampleSetData(){
        SampleSet selected = sampleSetList.getSelected();
        if (selected==null) return;
        sampleSetNameTextField.setText(selected.getName());
    }

    public void updateSampleData(){
        Sample selected = sampleList.getSelected();
        if (selected==null) return;
        String name = selected.getName() == null ? "" : selected.getName();
        float absTime = selected.getTime();
        sampleNameTextField.setText(name);
        sampleTimeTextField.setText(absTime+"");
    }

}
